package com.app.service.admin;

import com.app.SpringAnnotationDef;
import com.app.dao.admin.AdminDao;
import com.app.dao.admin.GorupManagerDao;
import com.app.dao.admin.UserDao;
import com.app.model.ORG_GorupResources;
import com.app.model.ORG_Resources;
import com.app.model.ORG_UserInfo;
import com.app.model.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: mosg
 * Date: 12-2-26
 * Time: 下午8:50
 * To change this template use File | Settings | File Templates.
 */

@Service(SpringAnnotationDef.USER_SERVICE)
public class UserService {

    @Resource(name = SpringAnnotationDef.USER_DAO)
    private UserDao dao;

    @Resource(name = SpringAnnotationDef.GORUPMANAGER_DAO)
    private GorupManagerDao gorupManagerDao;

    @Resource(name = SpringAnnotationDef.ADMIN_DAO)
    private AdminDao adminDao;


    public UserDao getDao() {
        return dao;
    }

    public void setDao(UserDao dao) {
        this.dao = dao;
    }

    public GorupManagerDao getGorupManagerDao() {
        return gorupManagerDao;
    }

    public void setGorupManagerDao(GorupManagerDao gorupManagerDao) {
        this.gorupManagerDao = gorupManagerDao;
    }

    public User loadAdmin(User user) {
        if ("admin".equals(user.getUsername())) {
            User admin = new User();
            admin.setUsername("admin");
            admin.setPassword("admin");
            admin.setSex("男");
            admin.setEname("admin");
            admin.setStatus(0);
            admin.setPositionId("1");
            admin.setPhone("13888888888");
            admin.setCreateDate(new Date());
            admin.setQq("111111111");
            admin.setEname("admin@localhost.com");
            this.getDao().save(admin);

            //创建密码
            ORG_UserInfo userInfo = new ORG_UserInfo();
            userInfo.setUserId(admin.getId());
            userInfo.setUsername("admin");
            //TODO:以后可以在系统里面配置 现在暂时这样
            userInfo.setPassword("888888");
            userInfo.setStatus(0);//0 没登陆  1登陆
            userInfo.setCreateTimeStamp(new Date());
            userInfo.setPasswordUpdateTime(new Date());
            userInfo.setUpdateTimeStamp(new Date());
            this.getDao().saveObject(userInfo);
            return admin;
        }
        return null;
    }

    /**
     * 获取当前登录人的资源菜单
     *
     * @param user
     * @return
     */
    public List<ORG_Resources> loadUserResources(User user) {
        //  List<ORG_Resources> resList = new ArrayList<ORG_Resources>();    //最终返回的
        Set<ORG_Resources> allSet = new HashSet<ORG_Resources>();     //去重复的父节点

        try {
            List<ORG_GorupResources> list = this.getGorupManagerDao().getGorupResources(Integer.parseInt(user.getPositionId()));
            for (ORG_GorupResources gRes : list) {
                //得到该菜单
                ORG_Resources oRes = this.getAdminDao().get(gRes.getResourcesId());
                if (oRes != null && oRes.getParentId() != 0) {
                    ORG_Resources parentRes = this.getAdminDao().get(oRes.getParentId());
                    //因为只有2级菜单，因此不需要递归了。
                    allSet.add(parentRes);
                }
                allSet.add(oRes);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ArrayList(allSet);
    }

    public AdminDao getAdminDao() {
        return adminDao;
    }

    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    public void updateUserOnline(User currentUser) {
        ORG_UserInfo userInfo = this.getDao().getUserInfo(currentUser.getId());
        if (userInfo != null) {
            userInfo.setStatus(1);
            this.getDao().saveObject(userInfo);
        }
    }

    public Map<String, String> loadUserResourcesCache(User currentUser) {
        Map<String, String> map = new HashMap<String, String>();
        List<ORG_Resources> list = this.getAdminDao().listAll();
        for (ORG_Resources res : list) {
            map.put(res.getId().toString(), res.getSort());
        }
        return map;  //To change body of created methods use File | Settings | File Templates.
    }
}
